package rise.net;

import rise.comunity.UserData;
import rise.message.Packet;
import rise.message.StatusNotification;
import android.content.Context;
import android.util.Log;

public class StatusNotificationTask extends SenderTask {
	private rise.comunity.Status status;

	public StatusNotificationTask(Context context, rise.comunity.Status status) {
		this(UserData.obtainUserData().getUsername(), status, context);
		Log.e("-", "Constructor");
	}

	public StatusNotificationTask(Context context) {
		this(context, UserData.obtainUserData().getStatus());
	}

	private StatusNotificationTask(String owner, rise.comunity.Status status,
			Context context) {
		this(new StatusNotification(owner, owner, status.toString(),
				OlivawPortsClient.getInputPort(), 0, 0), context);
		this.status = status;
	}

	protected StatusNotificationTask(Packet packet, Context context) {
		super(packet, context);
	}

	@Override
	protected Integer doInBackground(Void... params) {
		Log.e("-", "Doing stuff in backgroud");
		int r = 0;
		try {
			r = super.doInBackground(params);
		} catch (Throwable t) {
			Log.e("-", "THISDoInBack " + t.getMessage());
		}
		return r;
	}

	@Override
	protected void onPostExecute(Integer result) {
		try {
			if (result == Packet.CONNECTION_ERROR
					|| result == Packet.UNKNOWN_PACKET) {
				super.onPostExecute(result);
				return;
			}
		} catch (Throwable t) {
			Log.e("-", "THISonPostExecute " + t.getMessage());
		}

		if (result == Packet.OK) {
			UserData userData = UserData.obtainUserData();
			super.createSharedPreferences(userData.getUsername(),
					userData.getPassword(), status);
		}

	}

}